package com.ruoyi.web.controller.gcgl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.gcgl.domain.GysptPerson;
import com.ruoyi.gcgl.service.IGysptPersonService;
import com.ruoyi.common.config.RuoYiConfig;
import com.ruoyi.common.config.ServerConfig;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.file.FileUploadUtils;
import com.ruoyi.common.utils.file.FileUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 施工人员管理Controller
 * 
 * @author ruoyi
 * @date 2024-11-05
 */
@Controller
@RequestMapping("/gcgl/person")
public class GysptPersonController extends BaseController
{
    private String prefix = "gcgl/person";

    @Autowired
    private IGysptPersonService gysptPersonService;
    @Autowired
    private ServerConfig serverConfig;
    
    private static final String FILE_DELIMETER = ",";

    @RequiresPermissions("gcgl:person:view")
    @GetMapping()
    public String person()
    {
        return prefix + "/person";
    }

    /**
     * 查询施工人员管理列表
     */
    @RequiresPermissions("gcgl:person:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(GysptPerson gysptPerson)
    {
        startPage();
        List<GysptPerson> list = gysptPersonService.selectGysptPersonList(gysptPerson);
        return getDataTable(list);
    }

    /**
     * 导出施工人员管理列表
     */
    @RequiresPermissions("gcgl:person:export")
    @Log(title = "施工人员管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(GysptPerson gysptPerson)
    {
        List<GysptPerson> list = gysptPersonService.selectGysptPersonList(gysptPerson);
        ExcelUtil<GysptPerson> util = new ExcelUtil<GysptPerson>(GysptPerson.class);
        return util.exportExcel(list, "施工人员管理数据");
    }

    /**
     * 新增施工人员管理
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存施工人员管理
     */
    @RequiresPermissions("gcgl:person:add")
    @Log(title = "施工人员管理", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(GysptPerson gysptPerson)
    {
    	gysptPerson.setDeleteFlag(1);
    	gysptPerson.setCreateAt(new Date());
    	gysptPerson.setCreateUser(getLoginName());
    	gysptPerson.setUpdateAt(new Date());
    	gysptPerson.setUpdateUser(getLoginName());
        return toAjax(gysptPersonService.insertGysptPerson(gysptPerson));
    }

    /**
     * 修改施工人员管理
     */
    @RequiresPermissions("gcgl:person:edit")
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        GysptPerson gysptPerson = gysptPersonService.selectGysptPersonById(id);
        mmap.put("gysptPerson", gysptPerson);
        return prefix + "/edit";
    }

    /**
     * 修改保存施工人员管理
     */
    @RequiresPermissions("gcgl:person:edit")
    @Log(title = "施工人员管理", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(GysptPerson gysptPerson)
    {
    	gysptPerson.setUpdateAt(new Date());
    	gysptPerson.setUpdateUser(getLoginName());
        return toAjax(gysptPersonService.updateGysptPerson(gysptPerson));
    }

    /**
     * 删除施工人员管理
     */
    @RequiresPermissions("gcgl:person:remove")
    @Log(title = "施工人员管理", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(gysptPersonService.deleteGysptPersonByIds(ids));
    }
    
    /**
     * 通用上传请求（多个）
     */
    @PostMapping("/uploads")
    @ResponseBody
    public AjaxResult uploadFiles(List<MultipartFile> files) throws Exception{
        try
        {
            // 上传文件路径
            String filePath = RuoYiConfig.getUploadPath();
            List<String> urls = new ArrayList<String>();
            List<String> fileNames = new ArrayList<String>();
            List<String> newFileNames = new ArrayList<String>();
            List<String> originalFilenames = new ArrayList<String>();
            for (MultipartFile file : files)
            {
                // 上传并返回新文件名称
                String fileName = FileUploadUtils.upload(filePath, file);
                String url = serverConfig.getUrl() + fileName;
                urls.add(url);
                fileNames.add(fileName);
                newFileNames.add(FileUtils.getName(fileName));
                originalFilenames.add(file.getOriginalFilename());
            }
            AjaxResult ajax = AjaxResult.success();
            ajax.put("urls", StringUtils.join(urls, FILE_DELIMETER));
            ajax.put("fileNames", StringUtils.join(fileNames, FILE_DELIMETER));
            ajax.put("newFileNames", StringUtils.join(newFileNames, FILE_DELIMETER));
            ajax.put("originalFilenames", StringUtils.join(originalFilenames, FILE_DELIMETER));
            return ajax;
        }
        catch (Exception e)
        {
            return AjaxResult.error(e.getMessage());
        }
    }
}